package org.inspursc.s0517.health.common.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.inspursc.s0517.health.common.converter.RoleConverter2SysRoleDTO;
import org.inspursc.s0517.health.common.dto.SysRoleDTO;
import org.inspursc.s0517.health.common.entity.SysRole;
import org.inspursc.s0517.health.common.enums.ResultEnum;
import org.inspursc.s0517.health.common.exception.BizException;
import org.inspursc.s0517.health.common.form.RoleForm;
import org.inspursc.s0517.health.common.service.SysRoleService;
import org.inspursc.s0517.health.common.util.ResponseUtil;
import org.inspursc.s0517.health.common.vo.PageVO;
import org.inspursc.s0517.health.common.vo.ResponseVO;
import org.inspursc.s0517.health.common.vo.SysRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/role")
@Api(value = "角色操作的Controller")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @ApiOperation(value = "保存角色信息")
    @PostMapping("/save")
    public ResponseEntity<ResponseVO> save(@ApiParam("角色表单参数") @Valid RoleForm roleForm, BindingResult bindingResult) throws Exception{
        throw  new Exception("不允许该操作");
//        if (bindingResult.hasErrors()) {
//            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
//        }
//        SysRoleDTO sysRoleDTO = RoleConverter2SysRoleDTO.converter(roleForm);
//        SysRoleDTO roleDTO = roleService.save(sysRoleDTO, roleForm.getGroupIds());
//        if (null != roleDTO) {
//            return ResponseUtil.success("保存成功", roleDTO);
//        }
//        return ResponseUtil.error("保存失败", roleDTO);
    }

    @ApiOperation(value = "根据角色ID，删除角色信息")
    @GetMapping("/delete/{id}")
    public ResponseEntity<ResponseVO> delete(@ApiParam(value = "角色ID", required = true) @PathVariable Integer id) throws Exception{
        throw  new Exception("不允许该操作");
//        boolean is = roleService.deleteById(id);
//        if (is) {
//            return ResponseUtil.success("删除成功", is);
//        }
//        return ResponseUtil.error("删除失败", is);
    }


    @ApiOperation(value = "根据角色ID，获取角色信息")
    @GetMapping("/get/{id}")
    public ResponseEntity<ResponseVO> get(@ApiParam(value = "角色ID", required = true) @PathVariable Integer id) {

        SysRoleVO role = roleService.findById(id);
        if (null != role) {
            return ResponseUtil.success("", role);
        }
        return ResponseUtil.error("角色信息为空", role);
    }


    @ApiOperation(value = "根据查询条件，查询角色信息，并分页显示")
    @PostMapping(value = "/getByPage")
    public ResponseEntity<ResponseVO> getAll(@ApiParam(value = "角色名", required = false) @RequestParam String name,
                             @ApiParam(value = "当前页数", required = true) @RequestParam Integer pageIndex,
                             @ApiParam(value = "每页显示条数", required = true) @RequestParam Integer pageSize) {
        Pageable pageable = PageRequest.of(pageIndex - 1, pageSize);
        Page<SysRole> page = roleService.findByNameLike(name, pageable);
        PageVO pageVO = new PageVO(page.getContent(), page.getTotalPages(), page.getTotalElements(), pageIndex);
        return ResponseUtil.success("", pageVO);
    }


    @ApiOperation(value = "获取所有角色信息")
    @GetMapping(value = "/getAll")
    public ResponseEntity<ResponseVO> getAll() {
        List<SysRole> roleList = roleService.findAll();
        return ResponseUtil.success("", roleList);
    }
}
